package com.azt.data.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CopyObjectResult;
import com.aliyun.oss.model.ObjectMetadata;
import com.azt.api.model.AjaxJson;
import com.azt.api.model.OSSServer;

public class OSSFileUtils {

	
	public static String uploadImg(File file, String folder, String name, String extension) {
		try {
			return uploadFile(OSSServer.getImgServer(), new FileInputStream(file), file.length(), folder, name + "." + extension, "image/png");
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
	

	/**
	 * @param file
	 * @param folder
	 * @return
	 * @author 张栋  2016年5月9日下午2:55:06
	 */
	public static String uploadFile(File file, String folder) {
		String saveName = folder;
		OSSServer server = OSSServer.getImgServer();
		OSSClient client =null;
		try {
			FileInputStream fileInputStream = null;
			fileInputStream = new FileInputStream(file);

			client = new OSSClient(server.getEndpoint(), server.getKey(), server.getSecret());
			ObjectMetadata meta = new ObjectMetadata();
			meta.setContentLength(file.length());

			client.putObject(server.getBucket(), saveName, fileInputStream, meta);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}finally{
			client.shutdown();
		}
		return server.getAccessURL() + saveName;
	}

	public static String uploadFile(OSSServer server, InputStream in, long length, String folder, String name, String contentType) {
		OSSClient client = new OSSClient(server.getEndpoint(), server.getKey(), server.getSecret());
		ObjectMetadata meta = new ObjectMetadata();
		if (contentType != null) {
			meta.setContentType(contentType);
		}
		meta.setContentLength(length);

		String saveName = folder + "/" + name + "." + contentType;
		client.putObject(server.getBucket(), saveName, in, meta);
		return server.getAccessURL() + saveName;

	}

	public static AjaxJson deleteObject(String url) {
		OSSServer server = OSSServer.getImgServer();
		String key = getKey(url, server);
		AjaxJson j = new AjaxJson();
		OSSClient client = new OSSClient(server.getEndpoint(), server.getKey(), server.getSecret());
		try {
			boolean doesObjectExist = client.doesObjectExist(server.getBucket(),key);
			if(doesObjectExist){
				client.deleteObject(server.getBucket(),key);
			}else{
				j.setSuccess(false);
				j.setMsg("文件不存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
			j.setMsg("上传出错");
		}finally{
			client.shutdown();
		}
		return j;
	}

	
	public static String copy(String oldkey,String newkey){
		OSSServer server = OSSServer.getImgServer();
		OSSClient client = new OSSClient(server.getEndpoint(), server.getKey(), server.getSecret());
		CopyObjectResult copyObject = client.copyObject(server.getBucket(), oldkey, server.getBucket(), newkey);
		client.shutdown();
		return server.getAccessURL() + newkey;
	}
	
	public  static String getKey(String url, OSSServer server) {
		if(server==null){
			server = OSSServer.getImgServer();
		}
		String key = url.substring(server.getAccessURL().length());
		return key;
	}

}
